package com.itcast.springboot.service.impl;

import com.itcast.springboot.bean.SongList;
import com.itcast.springboot.bean.User;
import com.itcast.springboot.mapper.SongListMapper;
import com.itcast.springboot.mapper.UserMapper;
import com.itcast.springboot.service.SongListService;
import com.itcast.springboot.service.UserService;
import com.itcast.springboot.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
//对抛出的任何异常都进行自动回滚
@Transactional(rollbackFor = Exception.class)
public class SongListServiceImpl implements SongListService {

    @Autowired
    SongListMapper songListMapper;

    @Override
    public Integer deleteSongList(Integer slid) {
        Integer result = null;
        //根据slid查询出歌单的信息
        SongList songList = songListMapper.getSongListById(slid);
        //删除本地图片和歌曲
        Integer songdelete = FileUploadUtils.fileDelete(songList.getSongLink());
        Integer songPhotoDelete = FileUploadUtils.fileDelete(songList.getSongImg());
        //删除成功或者不存在
        if (songdelete >= 0 && songPhotoDelete >= 0) {
            result = songListMapper.deleteSongList(slid);
        }
        return result;
    }

    @Override
    public Integer insertSongListUpload(MultipartFile songFile, MultipartFile songPhoto, SongList songList) {
//        //文件未上传
//        if(songFile.isEmpty()){
//            return 0;
//        }else{
//            //获取原始图片名称
//            String songName = songFile.getOriginalFilename();
//            //取出歌曲的后缀
//            int index = songName.lastIndexOf(".");
//            String suffix = songName.substring(index + 1, songName.length());
//            //新的图片名字
//            String newSongName = UUID.randomUUID().toString() + "." + suffix;
//            System.out.println("新歌曲名称： " + newSongName);
//            //图片在项目中的全路径（绝对路径）
////            String songFullPath = System.getProperty("user.dir").concat("\\src\\main\\resources\\" +
////                    "static\\songs\\" + newSongName);
////            System.out.println("在项目中的绝对路径： " + songFullPath);
//            String filePath = "D://songs//";
//            //图片想要上传的本地路径位置
//            String songLocalPath = filePath.concat(newSongName);
//
//            //上传路径：在内存中创建File文件映射对象
//            File dest = new File(songLocalPath);
//            //父目录是否存在，不存在则创建
//            if (!dest.getParentFile().exists()) {
//                dest.getParentFile().mkdirs();
//            }
//            try {
//                //创建文件
//                songFile.transferTo(dest);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        if (songFile == null || songPhoto == null){
            return 0;
        }
        //上传歌曲到本地
        String songLocalPath = FileUploadUtils.fileUpload(0, songFile);
        //上传图片到本地
        String songPhotoLocalPath = FileUploadUtils.fileUpload(1, songPhoto);
        //上传失败
        if (songLocalPath == null || songPhotoLocalPath == null) {
            return 0;
        }
        //将歌曲的链接封装到SongList对象中
        songList.setSongLink(songLocalPath);
        songList.setSongImg(songPhotoLocalPath);
        return songListMapper.insertSongList(songList);
    }

    @Override
    public String insertSongFile(MultipartFile songFile) {
        if (songFile == null){
            return "上传歌曲不存在";
        }
        String serverPath = FileUploadUtils.fileUpload(0, songFile);
        if (serverPath == null){
            return "上传歌曲失败";
        }
        return serverPath;
    }

    @Override
    public String insertSongPhoto(MultipartFile songPhoto) {
        if (songPhoto == null){
            return "上传图片不存在";
        }
        String serverPath = FileUploadUtils.fileUpload(1, songPhoto);
        if (serverPath == null){
            return "上传图片失败";
        }
        return serverPath;
    }

    @Override
    public Integer insertSongList(SongList songList) {
        return songListMapper.insertSongList(songList);
    }

    @Override
    public SongList getSongListById(Integer slid) {
        SongList songList = songListMapper.getSongListById(slid);
        return songList;
    }

    @Override
    public List<SongList> getSongListsByIds(List<Integer> slids) {
        return songListMapper.getSongListsByIds(slids);
    }

    @Override
    public List<SongList> getSongListsRand(List<String> sts) {
        return songListMapper.getSongListsRand(sts);
    }

    @Override
    public List<SongList> getSongListsBySongTypeAndMood(String songType, String mood) {
        return songListMapper.getSongListsBySongTypeAndMood(songType, mood);
    }

    @Override
    public List<SongList> getSongListsByStAndNameAndAu(String songType, String songName, String author) {
        if (songType.equals("null") || songType.equals("undefined")){
            songType = null;
        }
        if (songName.equals("null") || songName.equals("undefined")){
            songName = null;
        }
        if (author.equals("null") || author.equals("undefined")){
            author = null;
        }
        return songListMapper.getSongListsByStAndNameAndAu(songType, songName, author);
    }


    @Override
    public Integer updateSongList(SongList songList) {
        SongList songList1 = null;
        if (songList.getSlid() != null){
            songList1 = songListMapper.getSongListById(songList.getSlid());
            String songLink = songList1.getSongLink();
            String songImg = songList1.getSongImg();
            Integer songLinkDelete = FileUploadUtils.fileDelete(songLink);
            Integer songImgDelete = FileUploadUtils.fileDelete(songImg);
            if (songImgDelete == -1 || songLinkDelete == -1){
                return -1;
            }
            return songListMapper.updateSongList(songList);
        }
        return -1;
    }
}
